package com.flyco.modules.sms.service.impl;

import com.flyco.modules.sms.entity.SmsBargain;
import com.flyco.modules.sms.entity.SmsBargainProduct;
import com.flyco.modules.sms.entity.SmsBargainRoom;
import com.flyco.modules.sms.entity.SmsBargainFollow;
import com.flyco.modules.sms.mapper.SmsBargainProductMapper;
import com.flyco.modules.sms.mapper.SmsBargainRoomMapper;
import com.flyco.modules.sms.mapper.SmsBargainFollowMapper;
import com.flyco.modules.sms.mapper.SmsBargainMapper;
import com.flyco.modules.sms.service.ISmsBargainService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 砍价
 * @Author: flyco
 * @Date:   2020-05-18
 * @Version: V1.0
 */
@Service
public class SmsBargainServiceImpl extends ServiceImpl<SmsBargainMapper, SmsBargain> implements ISmsBargainService {

	@Autowired
	private SmsBargainMapper smsBargainMapper;
	@Autowired
	private SmsBargainProductMapper smsBargainProductMapper;
	@Autowired
	private SmsBargainRoomMapper smsBargainRoomMapper;
	@Autowired
	private SmsBargainFollowMapper smsBargainFollowMapper;
	
	@Override
	@Transactional
	public void saveMain(SmsBargain smsBargain, List<SmsBargainProduct> smsBargainProductList,List<SmsBargainRoom> smsBargainRoomList,List<SmsBargainFollow> smsBargainFollowList) {
		smsBargainMapper.insert(smsBargain);
		for(SmsBargainProduct entity:smsBargainProductList) {
			//外键设置
			entity.setBargainId(smsBargain.getId());
			smsBargainProductMapper.insert(entity);
		}
		for(SmsBargainRoom entity:smsBargainRoomList) {
			//外键设置
			entity.setBargainId(smsBargain.getId());
			smsBargainRoomMapper.insert(entity);
		}
		for(SmsBargainFollow entity:smsBargainFollowList) {
			//外键设置
			entity.setBargainId(smsBargain.getId());
			smsBargainFollowMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(SmsBargain smsBargain,List<SmsBargainProduct> smsBargainProductList,List<SmsBargainRoom> smsBargainRoomList,List<SmsBargainFollow> smsBargainFollowList) {
		smsBargainMapper.updateById(smsBargain);
		
		//1.先删除子表数据
		smsBargainProductMapper.deleteByMainId(smsBargain.getId());
		smsBargainRoomMapper.deleteByMainId(smsBargain.getId());
		smsBargainFollowMapper.deleteByMainId(smsBargain.getId());
		
		//2.子表数据重新插入
		for(SmsBargainProduct entity:smsBargainProductList) {
			//外键设置
			entity.setBargainId(smsBargain.getId());
			smsBargainProductMapper.insert(entity);
		}
		for(SmsBargainRoom entity:smsBargainRoomList) {
			//外键设置
			entity.setBargainId(smsBargain.getId());
			smsBargainRoomMapper.insert(entity);
		}
		for(SmsBargainFollow entity:smsBargainFollowList) {
			//外键设置
			entity.setBargainId(smsBargain.getId());
			smsBargainFollowMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(Long id) {
		smsBargainProductMapper.deleteByMainId(id);
		smsBargainRoomMapper.deleteByMainId(id);
		smsBargainFollowMapper.deleteByMainId(id);
		smsBargainMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			smsBargainProductMapper.deleteByMainId(Long.valueOf( id.toString() ));
			smsBargainRoomMapper.deleteByMainId(Long.valueOf( id.toString() ));
			smsBargainFollowMapper.deleteByMainId(Long.valueOf( id.toString() ));
			smsBargainMapper.deleteById(id);
		}
	}
	
}
